package org.ddr.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class BasicServlet
 */

public abstract class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected static Logger log = Logger.getLogger("Servlet");
    
	protected String defaultMethodName = "execute";
	
	protected static String VIEW_PREFIX = "/WEB-INF/views";
	protected static String VIEW_APPENDIX = ".jsp";
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MainServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
		log.info("---------------------destroy--------------------------");
	}



	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		log.info("-----------------------init----------------------------");
	}



	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		log.info("----------------------board----------------------------");
		
		String ctxPath = request.getContextPath();
		String path = request.getPathInfo();
		String uri = request.getRequestURI();
		
		log.info("defaultMethodName:" + defaultMethodName);
		log.info("ctxPath:"+ctxPath);
		log.info("path:"+path);
		log.info("uri:"+uri);
		
		if(path == null || path.trim().length() == 0){
			path = defaultMethodName;
		}else{
			path = path.substring(1);
		}
		
		log.info("after: " + path);
		
		Class clz = this.getClass();
		try{
			Method targetMethod = clz.getMethod(path, HttpServletRequest.class,HttpServletResponse.class);
			
			log.info("targetMethod: " + targetMethod);
			
			targetMethod.invoke(this, request, response);
			
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
}
